package com.gec.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.entity.SysCourse;
import com.gec.model.entity.SysUser;
import com.gec.model.vo.SysCourseQueryVo;
import com.gec.system.service.SysCourseService;
import com.gec.system.service.SysCourseService;
import com.gec.system.util.Result;
import com.gec.system.util.VodTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author lxj
 * @since 2024-03-07
 */
@Api(tags = "课程详情控制器")
@RestController
@RequestMapping("/admin/system/sysCourse")
public class SysCourseController {

    @Autowired
    private SysCourseService sysCourseService;

    @Autowired
    private VodTemplate vodTemplate;

    @ApiOperation("获取全部")
    @GetMapping("/findAll")
    public Result findAll() {
        List<SysCourse> list = this.sysCourseService.list();
        return Result.ok(list);
    }

    @ApiOperation("根据id去移除")
    // 测试删除
    @DeleteMapping("/removeCourse/{id}")
    public Result removeCourse(@PathVariable Long id) {
        boolean b = this.sysCourseService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 分页和条件查询
    @GetMapping("/{page}/{limit}")
    public Result findCourseByPageQuery(@PathVariable Long page,
                                        @PathVariable Long limit,
                                        SysCourseQueryVo sysCourseQueryVo) {
        //1.创建分页对象
        IPage<SysCourse> p1 = new Page<SysCourse>(page, limit);
        //2.调用方法
        p1 = this.sysCourseService.selectPage(p1, sysCourseQueryVo);
        //3.返回
        return Result.ok(p1);
    }

    // 添加
    @PostMapping("/addCourse")
    public Result addCourse(@RequestBody SysCourse sysCourse) {
        boolean res = this.sysCourseService.save(sysCourse);
        if (res) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 修改
    //1.根据id
    @GetMapping("/findCourseById/{id}")
    public Result findCourseById(@PathVariable Long id) {
        SysCourse SysCourse = this.sysCourseService.getById(id);
        return Result.ok(SysCourse);
    }

    // 实现修改
    @PostMapping("/updateCourse")
    public Result updateCourse(@RequestBody SysCourse sysCourse) {
        boolean b = this.sysCourseService.updateById(sysCourse);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 批量删除
    @DeleteMapping("/removeCourseByIds")
    public Result removeCourseByIds(@RequestBody List<Long> ids) {
        boolean b = this.sysCourseService.removeByIds(ids);

        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    //    查找推荐的课程
    @ApiOperation("查找推荐的课程")
    @GetMapping("/findRecommend")
    public Result findRecommend() {
        QueryWrapper<SysCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("recommend", 1);
        List<SysCourse> list = this.sysCourseService.list(wrapper);
        return Result.ok(list);
    }

    // 更改课程状态
    @ApiOperation("更改课程状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer status) {
        this.sysCourseService.updateStatus(id, status);
        return Result.ok();
    }

    //    updateRecommend 是否推荐
    @ApiOperation("更改课程推荐")
    @GetMapping("/updateRecommend/{id}/{recommend}")
    public Result updateRecommend(@PathVariable Long id,
                                  @PathVariable Integer recommend) {
        this.sysCourseService.updateRecommend(id, recommend);
        return Result.ok();
    }


    @ApiOperation("根据id获取播放凭证")
    @GetMapping("/getPlayAuth/{id}")
    public Result getPlayAuth(@PathVariable Long id) throws Exception {

        // 1.根据id获取movie
        SysCourse sysCourse = this.sysCourseService.getById(id);

        //2.根据movie获取播放id
        String playId = sysCourse.getPlayId();

        //3.获取 封面
//        String image =  sysCourse.getImage();

        //4.根据播放id获取auth
        String playAuth = this.vodTemplate.getVideoPlayAuth(playId).getPlayAuth();

        Map<String, Object> map = new HashMap<>();
//        map.put("image", image);
        map.put("playId", playId);
        map.put("playAuth", playAuth);

        return Result.ok(map);
    }


}


